package io.github.uicdb.et;

import io.github.uicdb.et.register.ElementalObject;
import io.github.uicdb.et.register.IElementalGetter;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.EntityAttributeModificationEvent;
import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

public class ModAttributes {
    private static final DeferredRegister<Attribute> ATTRIBUTES = DeferredRegister.create(BuiltInRegistries.ATTRIBUTE, ElementalTheory.MODID);
    //易伤
    public static final Holder<Attribute> INJURE = ATTRIBUTES.register("attribute.elemental_theory.injure", () -> new RangedAttribute("injure", 0, 0, Integer.MAX_VALUE));
    //韧性
    public static final Holder<Attribute> TOUGHNESS = ATTRIBUTES.register("attribute.elemental_theory.element_toughness", () -> new RangedAttribute("attribute.element_toughness", 0, 0, 100));
    public static final Holder<Attribute> HEALTH_GETTER = ATTRIBUTES.register("attribute.elemental_theory.health_getter", () -> new RangedAttribute("attribute.health_getter", 0, 0, 1));
    public static final Holder<Attribute> HEALTH_RECOVERY = ATTRIBUTES.register("attribute.elemental_theory.health_recovery", () -> new RangedAttribute("attribute.health_recovery", 0, 0, Integer.MAX_VALUE));

    public static void init(IEventBus bus) {
        ATTRIBUTES.register(bus);
        bus.addListener(EntityAttributeModificationEvent.class, event -> {
            for (EntityType<? extends LivingEntity> type : event.getTypes()) {
                if (!event.has(type, INJURE)) event.add(type, INJURE);
                if (!event.has(type, TOUGHNESS)) event.add(type, TOUGHNESS);
                if (!event.has(type, HEALTH_GETTER)) event.add(type, HEALTH_GETTER);
                if (!event.has(type, HEALTH_RECOVERY)) event.add(type, HEALTH_RECOVERY);
            }
        });

    }

    public static float safeGet(Holder<Attribute> attribute, LivingEntity entity) {
        if (entity.getAttribute(attribute) != null) return (float) entity.getAttributeValue(attribute);
        else return 0;
    }

    public static float safeGetPrecent(Holder<Attribute> attribute, LivingEntity entity) {
        if (entity.getAttribute(attribute) != null) return (float) entity.getAttributeValue(attribute) / 100f;
        else return 0f;
    }

    public static class ElementAttribute extends RangedAttribute implements IElementalGetter {
        public ElementAttribute(String descriptionId, double defaultValue, double min, double max) {
            super(descriptionId, defaultValue, min, max);
        }


        ElementalObject elementalTheory$elementalObject;

        @Override
        public ElementalObject getElementalObject() {
            return elementalTheory$elementalObject;
        }

        @Override
        public void setElementalObject(ElementalObject elementalObject) {
            this.elementalTheory$elementalObject = elementalObject;
        }
    }
}
